package com.tiyende.kash.loop.utils

import android.content.Context
import android.content.res.AssetManager
import com.tiyende.kash.loop.TiyendeApp
import org.json.JSONObject
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader

/**
 * Assets 数据读取工具类
 */
object TiyendeAssetsJsonReader {

    /**
     * 从assets文件夹读取JSON文件并返回JSONObject
     * @param context 上下文
     * @param fileName assets中的文件名
     * @return JSONObject对象，如果出错返回null
     */
    fun readJsonFromAssets(context: Context, fileName: String): JSONObject? {
        return try {
            val jsonString = readTextFromAssets(context, fileName)
            jsonString?.let { JSONObject(it) }
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }

    /**
     * 从assets文件夹读取JSON文件并返回JSON字符串
     * @param context 上下文
     * @param fileName assets中的文件名
     * @return JSON字符串，如果出错返回null
     */
    private fun readTextFromAssets(context: Context, fileName: String): String? {
        var inputStream: InputStream? = null
        var reader: BufferedReader? = null
        val stringBuilder = StringBuilder()

        try {
            val assetManager: AssetManager = context.assets
            inputStream = assetManager.open(fileName)
            reader = BufferedReader(InputStreamReader(inputStream, "UTF-8"))

            var line: String?
            while (reader.readLine().also { line = it } != null) {
                stringBuilder.append(line)
            }

            return stringBuilder.toString()
        } catch (e: IOException) {
            e.printStackTrace()
            return null
        } finally {
            // 确保资源被关闭
            try {
                inputStream?.close()
                reader?.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }

    /**
     * 获取城市列表
     */
    fun readCityListFromAssets(): ArrayList<String>{
        val dataList = arrayListOf<String>()
        val jsonData = readJsonFromAssets(context = TiyendeApp.app,"json/custom_data.json")
        jsonData?.let { data ->
            val array = data.optJSONArray("city_array")
            array?.let {
                for (i in 0..< it.length()){
                    dataList.add(it.optString(i))
                }
            }
        }
        return dataList
    }

    /**
     * 获取resident_type
     */
    fun readResidentTypeListFromAssets(): ArrayList<String>{
        val dataList = arrayListOf<String>()
        val jsonData = readJsonFromAssets(context = TiyendeApp.app,"json/custom_data.json")
        jsonData?.let { data ->
            val array = data.optJSONArray("resident_type")
            array?.let {
                for (i in 0..< it.length()){
                    dataList.add(it.optString(i))
                }
            }
        }
        return dataList
    }

    /**
     * 获取 industry_type
     */
    fun readIndustryTypeListFromAssets(): ArrayList<String>{
        val dataList = arrayListOf<String>()
        val jsonData = readJsonFromAssets(context = TiyendeApp.app,"json/custom_data.json")
        jsonData?.let { data ->
            val array = data.optJSONArray("industry_type")
            array?.let {
                for (i in 0..< it.length()){
                    dataList.add(it.optString(i))
                }
            }
        }
        return dataList
    }




}